/*实现级联勾选开始*/
var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#tree').treeview('getSiblings', node.nodeId);
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}


//初始化表格参数
function fmtState(value) {
    return value==1?"已启用":"已禁用";
}

function fmtTime(value){
    return value.year+"-"+value.monthValue+"-"+value.dayOfMonth;
}

//角色授权
$(function () {
    $("#grant").dialog({
        title:'角色授权',
        btnName:'确认授权',
        btnBind:function () {
            var rows=$("#roleTb").bootstrapTable("getSelections");
            var params="roleId="+rows[0].id;
            var arr=$("#tree").treeview("getChecked");
            $(arr).each(function () {
                params=params+"&mids="+this.id;
            });
            $.post("sys/permission/grant4Role",params,function (msg) {
                toastr.warning(msg);
                $("#grant").dialog("close");
                $("#roleTb").bootstrapTable("uncheckAll")
            },'text');
        }
    })
});

function grant() {
    var arr=$("#roleTb").bootstrapTable("getSelections");
    if(arr.length!=1){
        swal({
            title:'信息提示',
            text:'请选择一项待授权（仅可选择一项）',
            type:'warning',
            confirmButtonText:'确定'
        });
        return;
    }
    $.getJSON("sys/permission/search4Tree","roleId="+arr[0].id,function (result) {
        $("#tree").treeview({
            data:result,
            showCheckbox:true,
            onNodeChecked:nodeChecked ,
            onNodeUnchecked:nodeUnchecked
        });
        $("#grant").dialog("open");
    })
}



//添加角色
$(function () {
    $("#add").dialog({
        title:'添加角色',
        btnName:'确认添加',
        btnBind:function () {
            var params=$("#add form").serialize();
            $.post("sys/js/add",params,function (msg) {
                toastr.warning(msg);
                $("#add").dialog("close");
                $("#roleTb").bootstrapTable("refresh");
            },'text');
        }
        });
});

function add() {
    $("#add form")[0].reset();
    $("#add").dialog("open");
}


//批量删除角色
function batchDel(){
    var arr=$("#roleTb").bootstrapTable("getSelections");
    if(arr.length==0){
        swal({
            title:'信息提示',
            text:'请至少选择一项待删除！',
            type:'warning',
            confirmButtonText:'确定'
        });
        return;
    }
    swal({
        title:'确定要删除吗？',
        text:'该操作不可逆！',
        type:'question',
        confirmButtonText:'确定',
        showCancelButton:true,
        cancelButtonText:'取消删除'
    }).then(function (falg) {
        if(falg){
            var params=$(arr).map(function () {
                return "ids="+this.id;
            }).get().join("&");

            $.post("sys/js/batchDel",params,function (msg) {
                toastr.warning(msg);
                $("#roleTb").bootstrapTable("refresh");
            },'text');
        }
    });
}


//启用或禁用角色
function optFmt(value,row){
    var state="禁用";
    var cla="danger";
    if(row.state ==0){
        state="启用";
        cla="primary";
    }
    var edit="<button class='btn btn-"+cla+" btn-sm edit' id='aa'>"+state+"</button>";
    var name="<button class=\"btn btn-info btn-sm name\">修改名称</button>";
    return edit+"&nbsp;&nbsp"+name;
}

$(function () {
    $("#updateName").dialog({
        title:'修改角色名称',
        btnName:'确认修改',
        btnBind:function () {
            var params=$("#updateName form").serialize();
            $.post("sys/js/updateName",params,function (msg) {
                toastr.warning(msg);
                $("#updateName").dialog("close");
                $("#roleTb").bootstrapTable("refresh");
            });
        }
    });
});
var optEvent={
    "click .edit":function (e, value, row, index) {
        var params="id="+row.id+"&state="+row.state;
        $.post("sys/js/updateState",params,function (msg) {
            toastr.warning(msg);
            $("#roleTb").bootstrapTable("refresh");
        },'text');
    },
    "click .name":function (e, value, row, index) {
        $("[name=id]").val(row.id);
        $("[name=name]").val(row.name);
        $("#updateName").dialog("open");
    }
}